package com.bjsxt.userscenter.back.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.controller.BaseController;
import com.bjsxt.userscenter.common.util.PageInfoUtil;
import com.bjsxt.userscenter.system.pojo.ARegion;
import com.bjsxt.userscenter.system.service.ISystemService;

/**
 * 用户登陆后的主页面
 * 
 * @author wangsh_sxt
 *
 */
@Controller
@RequestMapping("/back/system/")
public class SystemBackController extends BaseController
{
	@Autowired
	private ISystemService systemService;

	/**
	 * 地区列表
	 * 
	 * @return
	 */
	@RequestMapping("/regionList")
	public String regionList(HttpServletRequest request)
	{
		/* 接收参数 */
		String parentId = request.getParameter("parentId");
		String keyword = request.getParameter("keyword");
		String status = request.getParameter("status");
		String st = request.getParameter("st");
		String ed = request.getParameter("ed");

		if (keyword == null)
		{
			keyword = "";
		}
		keyword = keyword.trim();
		if (parentId == null)
		{
			parentId = "";
		}
		if (status == null)
		{
			status = "";
		}
		if (st == null)
		{
			st = "";
		}
		if (ed == null)
		{
			ed = "";
		}

		Date stDate = null;
		Date edDate = null;
		if (!"".equalsIgnoreCase(st) && !"".equalsIgnoreCase(ed))
		{
			stDate = this.dateFormatUtil.parse(st);
			edDate = this.dateFormatUtil.parse(ed);
		}

		/* 查询数据 */
		PageInfoUtil pageInfoUtil = this.proccedPageInfo(request);
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("keyword", "%" + keyword + "%");
		condMap.put("parentId", parentId);
		condMap.put("status", status);
		condMap.put("st", stDate);
		condMap.put("ed", edDate);
		List<ARegion> regionList = this.systemService.findCondListRegionService(pageInfoUtil, condMap);

		/* 存储结果 */
		request.setAttribute("regionList", regionList);
		request.setAttribute("pageInfoUtil", pageInfoUtil);
		return "/back/system/regionList";
	}

	/**
	 * 地区添加
	 * 
	 * @return
	 */
	@RequestMapping("/regionInsert")
	public String regionInsert(HttpServletRequest request,ARegion region)
	{
		String parentId = request.getParameter("parentId");
		if(parentId == null)
		{
			parentId = "" ; 
		}
		if("".equalsIgnoreCase(parentId))
		{
			parentId = "0";
		}
		request.setAttribute("parentId", parentId);
		return "/back/system/regionInsert";
	}

	/**
	 * 地区添加提交
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value ="/regionInsertSubmit",produces="text/html;charset=UTF-8")
	public String regionInsertSubmit(HttpServletRequest request, HttpServletResponse response,
			ARegion region)
	{
		/* 处理发布时间 */
		String pubTime = request.getParameter("pubTimeCond");
		region.setPubTime(this.dateFormatUtil.parse(pubTime));
		region.setCreateTime(new Date());
		region.setUpdateTime(new Date());
		JSONObject dbJSON = this.systemService.saveOneRegionService(region);
		info = dbJSON.getString("info");
		
		JSONObject resultJSON = this.printJUIString(request, info);
		return resultJSON + "";
	}
	
	/**
	 * 地区更新
	 * 
	 * @return
	 */
	@RequestMapping("/regionUpdate")
	public String regionUpdate(HttpServletRequest request)
	{
		String rid = request.getParameter("rid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", rid);
		ARegion region = this.systemService.findOneRegionService(condMap);
		request.setAttribute("region", region);

		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			return "/back/system/regionUpdate";
		}
		return "/back/system/regionInfo";
	}

	/**
	 * 地区更新提交
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/regionUpdateSubmit",produces="text/html;charset=UTF-8")
	public String regionUpdateSubmit(HttpServletRequest request, HttpServletResponse response)
	{
		String rid = request.getParameter("rid");
		Map<String, Object> condMap = new HashMap<String, Object>();
		condMap.put("id", rid);
		ARegion region = this.systemService.findOneRegionService(condMap);

		boolean updateFlag = false;
		String operType = request.getParameter("operType");
		if ("update".equalsIgnoreCase(operType))
		{
			String name = request.getParameter("name");
			String areacode = request.getParameter("areacode");
			String pinyin = request.getParameter("pinyin");
			String content = request.getParameter("content");
			String status = request.getParameter("status");
			
			/* 处理发布时间 */
			String pubTime = request.getParameter("pubTimeCond");
			region.setPubTime(this.dateFormatUtil.parse(pubTime));
			
			region.setName(name);
			region.setAreacode(areacode);
			region.setPinyin(pinyin);
			region.setContent(content);
			region.setStatus(Byte.valueOf(status));
			updateFlag = true;
		}

		if (updateFlag)
		{
			region.setUpdateTime(new Date());
			JSONObject dbJSON = this.systemService.updateOneRegionService(region);
			if (dbJSON != null)
			{
				info = dbJSON.getString("info") + "";
			}
		}
		JSONObject resultJSON = this.printJUIString(request, info);
		return resultJSON + "" ;
	}

	/**
	 * 地区的批处理
	 * 
	 * @return
	 */
	@RequestMapping("/regionBatch")
	public String regionBatch(HttpServletRequest request, HttpServletResponse response)
	{
		return null;
	}
}
